package com.foolishbird.sdk.params;

import android.text.TextUtils;

import com.foolishbird.sdk.secret.MD5;
import com.foolishbird.sdk.utils.WL;
import com.foolishbird.sdk.utils.XmlUtils;


public final class GetRequestParams {
    private static final String TAG = GetRequestParams.class.getSimpleName();

    private static final String DEFAULET_SECRET = "86b070f9bcc5a83178fd4ed51ec7fd6b";

    final String service;
    final String sign;
    final long time;
    final String game_id;
    final String uid;
    final String ticket;

    private GetRequestParams(final Builder builder) {
        this.time = builder.time;
        this.sign = builder.sign;
        this.service = builder.service;
        this.uid = builder.uid;
        this.game_id = builder.game_id;
        this.ticket = builder.ticket;
    }

    public String toParamsString(String httpPrefix) {
        StringBuilder builder = new StringBuilder(httpPrefix);
        builder.append("?service=").append(service);
        builder.append("&sign=").append(sign);
        builder.append("&request_time=").append(time);
        builder.append("&game_id=").append(game_id);
        builder.append("&uid=").append(uid);
        builder.append("&ticket=").append(ticket);
        WL.d(TAG, "toParamsString : " + builder.toString());
        return builder.toString();
    }

    public static class Builder {
        private String service;
        private String sign;
        private long time;
        private String game_id;
        private String uid;
        private String secretKey;
        private String ticket;

        public Builder() {
            this.time = System.currentTimeMillis() / 1000;
            setSecretKey(XmlUtils.getPlatformCustomKey());
        }

        public Builder setService(String service){
            this.service = service;
            return this;
        }

        public Builder setGameId(String gameId) {
            this.game_id = gameId;
            return this;
        }

        public Builder setUid(String uid) {
            this.uid = uid;
            return this;
        }

        public Builder setSecretKey(String secretKey){
            this.secretKey = secretKey;
            return this;
        }

        public Builder setTicket(String ticket) {
            this.ticket = ticket;
            return this;
        }

        public GetRequestParams build() {
            StringBuilder builder = new StringBuilder();
            builder.append("service=").append(service);
            builder.append("&request_time=").append(time);
            builder.append("&game_id=").append(game_id);
            builder.append("&uid=").append(uid);
            builder.append("&ticket=").append(ticket);
            builder.append("&key=").append(TextUtils.isEmpty(secretKey) ? DEFAULET_SECRET : secretKey);
            WL.d(TAG, "Builder sign string: " + builder.toString());
            this.sign = MD5.encrypt(builder.toString()).toLowerCase();
            return new GetRequestParams(this);
        }

    }


}
